package com.rongjih.learn.webflux;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@SpringBootApplication
public class App {
  @Autowired
  private StudentService studentService;

  public static void main(String[] args) {
    SpringApplication.run(App.class, args);
  }

  @Bean
  public RouterFunction<ServerResponse> routerFunction() {
    System.out.println("fn: thread.id=" + Thread.currentThread().getId());
    /*
    HandlerFunction<ServerResponse> handlerFunction =
      (request) -> ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM).body(all(), Student.class);
    RequestPredicate predicate = RequestPredicates.GET("/students");
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route(predicate, handlerFunction);
    */

    return route(GET("/students"),
      (request) -> ServerResponse.ok().contentType(MediaType.TEXT_EVENT_STREAM).body(studentService.all(), Student.class));
  }
}